//
//  GFLikeUserTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFLikeUserTableViewCell.h"

@interface GFLikeUserTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *likeUserCollection;

@end

@implementation GFLikeUserTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.itemSize = CGSizeMake((ScreenWidth - 70) / 6, (ScreenWidth - 70) / 6);
        flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flow.minimumInteritemSpacing = 10;
        flow.minimumLineSpacing = 10;
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.likeUserCollection = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:flow];
        self.likeUserCollection.backgroundColor = [UIColor whiteColor];
        self.likeUserCollection.scrollEnabled = NO;
        self.likeUserCollection.delegate = self;
        self.likeUserCollection.dataSource = self;
        self.likeUserCollection.userInteractionEnabled = NO;
        [self.contentView addSubview:self.likeUserCollection];
        [self.likeUserCollection registerClass:[GFLikeUserCollectionViewCell class] forCellWithReuseIdentifier:@"likeUser"];
        
    }
    return self;
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.likeUserArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GFLikeUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"likeUser" forIndexPath:indexPath];
    cell.tempDic = [self.likeUserArr objectAtIndex:indexPath.item];
    return cell;
    
}

-(void)setLikeUserArr:(NSArray *)likeUserArr {
    
    if (_likeUserArr != likeUserArr) {
        _likeUserArr = likeUserArr;
    }
    self.likeUserCollection.frame = CGRectMake(0, 0, ScreenWidth, [[self class] heightForLikeUserImageView:likeUserArr.count]);
    [self.likeUserCollection reloadData];
}

+(CGFloat)heightForLikeUserImageView:(NSInteger)count {
    
    if (count % 6 == 0) {
        
        CGFloat h = count / 6 * ((ScreenWidth - 70) / 6) + 10 * (count / 6 + 1);
        return h;
    } else {
        
        CGFloat h = (count / 6 + 1) * ((ScreenWidth - 70) / 6) + 10 * (count / 6 + 1);
        return h;
    }
    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
